package com.risenb.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.risenb.bean.Menu;
import com.risenb.dao.MenuMapper;
import com.risenb.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	MenuMapper menuMapper;
	
	@Override
	public int removeMenuById(Integer id) {
		return menuMapper.removeMenuById(id);
	}

	@Override
	public int createMenu(Menu record) {
		return menuMapper.createMenu(record);
	}

	@Override
	public Menu queryMenuById(Integer id) {
		return menuMapper.queryMenuById(id);
	}

	@Override
	public int modifyMenuById(Menu record) {
		return menuMapper.modifyMenuById(record);
	}

	@Override
	public List<Menu> queryMenus() {
		return menuMapper.queryMenus();
	}

	@Override
	public List<Menu> queryMenusByParentid(Integer parentid) {
		return menuMapper.queryMenusByParentid(parentid);
	}

	@Override
	public int removeMenuByIds(String ids) {
		List<Menu> ms = queryMenusByParentidRecur(ids);
		String [] idss = new String[ms.size()];
		for (int i = 0; i < ms.size(); i++) {
			idss[i] = ms.get(i).getId()+"";
		}
		return menuMapper.removeMenuByIds(toString(idss));
	}

	@Override
	public List<Menu> queryMenusByParentidRecur(String ids) {
		return menuMapper.queryMenusByParentidRecur(ids);
	}
	protected String toString(String [] ids){
		String str = "";
		for (int i = 0; i < ids.length; i++) {
			str+=ids[i]+",";
		}
		return str.substring(0,str.length()-1);
	}
}
